 <!--
* description: 保修申请-提报
* author: chenjingwen
* createdDate: 2021-2-22
-->
<template>
  <div class="app-container app-container-table">
    <one-table-template
      ref="multipleTable"
      :key="oneTableKey"
      :dynamic-buttons="tableButtons"
      :dynamic-components="tableComponents"
      :dynamic-table-cols="tableCols"
      :dynamic-api-config="apiConfig"
      :dynamic-form-fields="formField"
      :dynamic-is-show-select="false"
    />

    <Edit
      :key="editPopupsKey"
      :dynamic-edit-row-data="editRowData"
      :popups-visible="editPopupsVisible"
      :popups-state="editPopupsState"
      @close="close"
    />

    <claimDetails
      :key="detailsPopupsKey"
      :dynamic-edit-row-data="detailsRowData"
      :popups-visible.sync="detailsPopupsVisible"
      :popups-state="editPopupsState"
      @closeviewData="closeviewData"
    />
  </div>
</template>
<script>
import { oneTableWithViewTemplateMixins } from '@/components/mixins/oneTableWithViewTemplateMixins'
import OneTableTemplate from '@/components/templates/oneTable'
import Vue from 'vue'
import { vehiclePdiPriceInquirys } from '@/api/se/vehiclePdiPriceInquiry'

export default {
  name: 'ClaimApplication',

  components: {
    OneTableTemplate,
    Edit: () => import('./edit'),
    claimDetails: () => import('./detail')

  },

  // 组件混入对象：{data[listQuery] methods[queryTable]}
  mixins: [oneTableWithViewTemplateMixins],

  // 阻塞路由预加载网格中组件的数据
  beforeRouteEnter(to, from, next) {
    Vue.prototype.$ConfigCache.CacheConfig.initData([to.path, ''], function() {
      next()
    })
  },

  data() {
    // 页面标识（与上面beforeRouteEnter中的对应）
    const viewPageCode = this.$route.path

    // 绑定事件配置化事件
    this.$ConfigCache.CacheConfig.bindEvent(this, [viewPageCode])

    return {
      // 网格查询API配置对象
      apiConfig: vehiclePdiPriceInquirys.findClaimList,
      editRowData: {},
      editRowData1: {},
      editPopupsVisible: false,

      detailsPopupsKey: '1',
      detailsRowData: {},
      detailsPopupsVisible: false,

      // 动态组件-按钮
      tableButtons: [
        {
          compKey: 'btnKey3',
          size: 'small',
          name: 'add',
          clickEvent: () => this.add(),
          text: this.$t('sys.button.add'),
          position: 'left' // 新增
        },

        {
          compKey: 'btnKey3',
          type: 'primary',
          size: 'small',
          name: 'search',
          clickEvent: () => this.queryTable(1),
          text: this.$t('sys.button.query'), // 查询
          position: 'right',
          fuzzySearch: true
        },
        {
          compKey: 'btnKey4',
          size: 'small',
          name: 'reset',
          clickEvent: () => this.reset(),
          text: this.$t('sys.button.reset'),
          position: 'right' // 重置
        }
      ],
      // 动态组件-查询条件
      tableComponents:
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path] &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
          .tableComponents &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
          .tableComponents.length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
            .tableComponents
          : [
            {
              compKey: 'compKey1',
              labelName: '申请单号：',
              codeField: 'preAuthorCode',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMul: false,
              isMust: true
            },
            {
              compKey: 'compKey2',
              labelName: 'VIN码：',
              codeField: 'vin',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMul: false,
              isMust: true
            },
            {
              compKey: 'compKey3',
              labelName: '车牌号：',
              codeField: 'carLicense',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMul: false,
              isMust: true
            },
            {
              compKey: 'compKey4',
              labelName: '申请日期：',
              codeField: 'sendStartTime,sendEndTime',
              component: () => import('@/components/org/datePicker/datePicker'),
              dateType: 'daterange',
              isMul: false,
              isMust: true
            },
            {
              compKey: 'compKey5',
              labelName: '保修类别：',
              codeField: 'guranteeType',
              component: () => import('@/components/org/commonSelect'),
              customObj: {
                api: vehiclePdiPriceInquirys.findGuranteeType,
                selectType: '1'
              },
              oFields: 'guranteeType,guranteeTypeName',
              isMul: false,
              isMust: false
            },
            {
              compKey: 'key7',
              labelName: '状态：',
              codeField: 'sendStatus',
              component: () => import('@/components/org/commonSelect'),
              customObj: {
                api: vehiclePdiPriceInquirys.findClaimStutas,
                selectType: '1'
              },
              oFields: 'sendStatus,sendStatusName',
              isMul: false,
              isMust: false
            }
          ],
      // 动态生成网格列
      tableCols:
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path] &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols
          .length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols
          : [
            {
              prop: 'subsidyId',
              label: '主键ID',
              hidden: true,
              alwaysHidden: true
            },
            {
              prop: 'updateControlId',
              label: '并发控制ID',
              hidden: true,
              alwaysHidden: true
            },
            {
              prop: 'controlBtn',
              label: '操作',
              width: 100,
              fixed: true,
              isComponent: true,
              comps: [
                {
                  compKey: 'propKey0',
                  labelName: '编辑',
                  component: () => import('@/components/org/linkButton'),
                  rowFileds: 'show:sendStatus-录入|退回',
                  clickEvent: index => this.edit(index)
                },
                {
                  compKey: 'propKey1',
                  labelName: '删除',
                  component: () => import('@/components/org/linkButton'),
                  rowFileds: 'show:sendStatus-录入',
                  clickEvent: index => this.del(index)
                },
                {
                  compKey: 'propKey2',
                  labelName: '查看',
                  component: () => import('@/components/org/linkButton'),
                  clickEvent: (index) => this.claimDetails(index)
                }
              ]
            },
            {
              prop: 'preAuthorCode',
              label: '申请单号'
            },
            {
              prop: 'sendStatus',
              label: '状态'
            },
             {
              prop: 'agreeNo',
              label: '同意单号'
            },
            {
              prop: 'custName',
              label: '客户姓名'
            },
            {
              prop: 'vin',
              label: 'VIN码'
            },
            {
              prop: 'guranteeTypeName',
              label: '保修类别'
            },
            {
              prop: 'checkMan',
              label: '审核人'
            },
            {
              prop: 'sendTime',
              label: '申请日期'
            }
          ],

      // 表单数据
      formField: this.$utils.getFormField(
        this,
        {
          preAuthorCode: '',
          vin: '',
          carLicense: '',
          sendStartTime: '',
          sendEndTime: '',
          guranteeType: '',
          sendStatus: ''
        },
        this.$ConfigCache.CacheConfig.cacheData[viewPageCode]
      )
    }
  },

  mounted() {
    // 列表查询
    this.queryTable(1)
  },

  methods: {
    edit(index) {
      this.editPopupsState = 'edit'
      this.editPopupsVisible = true
      this.editRowData = this.$refs['multipleTable'].list[index]
    },
    add() {
      this.editPopupsState = 'add'
      this.editPopupsVisible = true
    },
    claimDetails(index) {
      this.editPopupsState = 'edit'
      this.detailsPopupsVisible = true
      this.detailsRowData = this.$utils.deepCopy(
        this.$refs['multipleTable'].list[index]
      )
    },
    // // 关闭资料查看弹窗
    closeviewData() {
      this.detailsPopupsVisible = false
      this.detailsPopupsKey = this.$utils.generateId()
    },
    // 删除
    del(index) {
      this.$confirm('确定要删除吗？', '提示', {
        type: 'warning'
      }).then(() => {
        const obj = this.$refs.multipleTable.list[index]
        const queryObj = {
          type: 'mutation',
          apiConfig: vehiclePdiPriceInquirys.deleteClaim,
          variables: {
            preAuthorID: obj.preAuthorID
          }
        }
        var paramD = this.$getParams(queryObj)
        this.$requestAPI(paramD).then(response => {
          if (response.result === '1') {
            this.$message({
              message: response.msg,
              type: 'success',
              duration: 1000
            })
            this.$nextTick(() => {
              this.queryTable(1)
            })
          } else {
            this.$message({
              message: response.msg,
              type: 'warn',
              duration: 1000
            })
          }
        })
      })
    },
    close() {
      this.editPopupsVisible = false
      this.queryTable(1)
    }
  }
}
</script>
